﻿using System;
using System.Configuration;
using System.Web.Configuration;

namespace Shuba.Web.appsetup
{
    public partial class setup : basePage
    {
        string msg;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                this.Form.DefaultFocus = txtusername.ClientID;
                Panel1.Visible = false;
            }
        }

        private bool IsValidUser()
        {
            Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();
            
            string user1 = ConfigurationManager.AppSettings["admin1.name"];
            string user2 = ConfigurationManager.AppSettings["admin2.name"];
            string ULoginName = txtusername.Text.Trim().ToUpper();

            bool IsLDAPUSer = CheckLDAPUser(ULoginName, txtPassword.Text);

            if (IsLDAPUSer)
            {
                ViewState["UserPass"] = txtPassword.Text;
                if (user1 != "")
                {
                    if (user1 == ULoginName || user2 == ULoginName)
                    {
                        Panel1.Visible = true;
                        UpdatePanel1.Visible = true; 
                        Panel2.Visible = false;
                        return true;
                    }
                    else
                    {
                        Panel1.Visible = false;
                        UpdatePanel1.Visible = true;
                        Panel2.Visible = true;
                        return false;
                    }
                }
                else
                {
                    Panel1.Visible = true;
                    UpdatePanel1.Visible = true;
                    Panel2.Visible = false;
                    return true;
                }
            }
            else
            {
                msg = this.GetGlobalResourceObject("Messages", "FailedLoginToLDAP").ToString();
                Alert(msg);
                return false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string ULoginName = txtusername.Text.Trim().ToUpper(); 
            string LDAPServer = txtLDAP.Text;
            string LDAPUserPass = ViewState["UserPass"].ToString();
            string LDAPUserName = CheckLDAPUser(LDAPServer, ULoginName, LDAPUserPass, txtAdmin1Name.Text);

            if (LDAPUserName != "")
            {
                LDAPUserName = CheckLDAPUser(LDAPServer, ULoginName, LDAPUserPass, txtAdmin2Name.Text);
                if (LDAPUserName != "")
                {
                    SaveConfig();
                    string msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                    AlertAndRedirect(msg, txtAddress.Text);
                }
                else
                {
                    msg = "Administrator User Should Be Registered in LDAP Server";
                    Alert(msg);
                }
            }
            else
            {
                msg = "Administrator User Should Be Registered in LDAP Server";
                Alert(msg);
            }
        }

        private void SaveConfig()
        {
            Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();
            Configuration WebConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConnectionStringsSection ConnStringSec = WebConfig.ConnectionStrings;

            WebConfig.AppSettings.Settings["app.code"].Value = txtAppCode.Text.Trim().ToUpper() ;
            WebConfig.AppSettings.Settings["title"].Value = txtAppName.Text.Trim();
            
            WebConfig.AppSettings.Settings["address"].Value = txtAddress.Text;

            WebConfig.AppSettings.Settings["ldap"].Value = txtLDAP.Text;

            WebConfig.AppSettings.Settings["admin1.name"].Value = txtAdmin1Name.Text.Trim().ToUpper();
            WebConfig.AppSettings.Settings["admin2.name"].Value = txtAdmin2Name.Text.Trim().ToUpper();
            WebConfig.AppSettings.Settings["db.server"].Value = txtDbServer.Text;
            WebConfig.AppSettings.Settings["db.name"].Value = txtDbName.Text;
            WebConfig.AppSettings.Settings["db.user"].Value = txtDbUser.Text;
            WebConfig.AppSettings.Settings["db.pwd"].Value = Encryption.Encrypt(txtDbPassword.Text);

            WebConfig.Save();
        }

        private void LoadConfig()
        {
           Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();

           txtAppCode.Text = ConfigurationManager.AppSettings["app.code"];
           txtAppName.Text = ConfigurationManager.AppSettings["title"];
           txtAddress.Text = ConfigurationManager.AppSettings["address"];

           txtLDAP.Text = ConfigurationManager.AppSettings["ldap"];
           txtAdmin1Name.Text = ConfigurationManager.AppSettings["admin1.name"];

           txtAdmin2Name.Text = ConfigurationManager.AppSettings["admin2.name"];
           txtDbServer.Text = ConfigurationManager.AppSettings["db.server"];
           txtDbName.Text = ConfigurationManager.AppSettings["db.name"];
           txtDbUser.Text = ConfigurationManager.AppSettings["db.user"];

           if (ConfigurationManager.AppSettings["db.pwd"].Trim() != "")
           {
               txtDbPassword.Attributes.Add("value", Encryption.Decrypt(ConfigurationManager.AppSettings["db.pwd"]));
           }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if(IsValidUser())
            {
                if(ConfigurationManager.AppSettings["admin1.name"] != "") LoadConfig();
            }
            else
            {
                Alert("Invalid username and password combination.");
            }
        }


        protected void btnKey_Click(object sender, EventArgs e)
        {
            Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();
            enckey_result.Text = Encryption.Encrypt(enckey.Text.Trim().Replace(",", ";"));
            UpdatePanel1.Visible = true;
            UpdatePanel1.Update();
        }
    }
}
